import { setStore } from "@/utils/store"
import { chartConfig } from "@/page/chart/options"

const initSignData = () => {
  return {
    id: 0,
    remark: "",
    longitude: 0,
    latitude: 0
  }
}

const chart = {
  state: {
    signDialogShow: false,
    signDialogData: initSignData(),
    shipSetting: {},
    allShipList: [], // 本船队船只
    shipList: [], // 本船队海图中的船只
    otherShipList: [],
    alarmLoading: false,
    alarmInfo: null,
    weatherValue: "",
    typhoonList: []
  },
  actions: {
    OpenSignDialog({ commit }) {
      commit('OPEN_SIGN_DIALOG', true);
    },
    closeSignDialog({ commit }) {
      commit('CLOSE_SIGN_DIALOG')
    },
    refreshShipList({ commit }, list) {
      commit('REFRESH_SHIP_LIST', list)
    },
    refreshShipListByMmsi({ commit }, row) {
      commit('REFRESH_SHIP_LIST_BY_MMSI', row)
    },
    refreshOtherShipList({ commit }, list) {
      commit('REFRESH_OTHER_SHIP_LIST', list)
    },
    refreshAllShipList({ commit }, list) {
      commit('REFRESH_ALL_SHIP_LIST', list)
    },
    setWeatherValue({ commit }, list) {
      commit('SET_WEATHER_VALUE', list)
    },
    setTyphoonList({ commit }, list) {
      commit('SET_TYPHOON_LIST', list)
    },
  },
  mutations: {
    // 标记的弹框
    OPEN_SIGN_DIALOG: (state, data) => {
      state.signDialogData = Object.assign({}, state.signDialogData, data)
      state.signDialogShow = true
    },
    CLOSE_SIGN_DIALOG: (state) => {
      state.signDialogShow = false
      state.signDialogData = initSignData()
    },
    // 船舶显示设置
    CHANGE_SHIP_SETTING: (state, data) => {
      setStore({
        name: chartConfig.shipSettingName,
        content: data
      })
      state.shipSetting = data
    },
    REFRESH_ALL_SHIP_LIST: (state, list) => {
      state.allShipList = list
    },
    // 更新船队列表
    REFRESH_SHIP_LIST: (state, list) => {
      state.shipList = list
    },
    REFRESH_SHIP_LIST_BY_MMSI: (state, row) => {
      const index = state.shipList.findIndex(m => m.mmsi === row.mmsi)
      if (index === -1) {
        state.shipList.push(row)
      } else {
        state.shipList.splice(index, 1, row)
      }
    },
    REFRESH_OTHER_SHIP_LIST: (state, list) => {
      state.otherShipList = list
    },
    // 提交报警区域信息
    SUBMIT_ALARM_INFO: (state, row) => {
      state.alarmInfo = row
      state.alarmLoading = true
    },
    CLEAR_ALARM_INFO: (state) => {
      state.alarmLoading = false
      state.alarmInfo = null
    },
    SET_WEATHER_VALUE: (state, value) => {
      state.weatherValue = value
    },
    SET_TYPHOON_LIST: (state, list) => {
      state.typhoonList = list
    },
  }
};

export default chart;
